const _ = require('lodash')
const STRING_STAR = '******'

/**
 * 手机号脱敏
 * @param mobile 手机号
 * @return 脱敏后的手机号
 */
export function desensitizeMobile(mobile) {
    if (!_.isEmpty(mobile) && mobile.length > 7) {
        mobile = mobile.substr(0, 3) + STRING_STAR + mobile.substr(mobile.length - 3)
    }
    return mobile
}

/**
 * 身份证脱敏
 * @param idCard 身份证号
 * @return 脱敏后的身份证号
 */
export function desensitizeIdCard(idCard) {
    if (!_.isEmpty(idCard) && idCard.length >= 15) {
        idCard = idCard.substr(0, 2) + STRING_STAR + idCard.substr(idCard.length - 4)
    }
    return idCard
}

/**
* 银行卡脱敏
* @param bankCard 银行卡号
* @return 脱敏后的银行卡号
*/
export function desensitizeBankCard(bankCard) {
    if (!_.isEmpty(bankCard) && bankCard.length >= 10) {
        bankCard = bankCard.substr(0, 4) + STRING_STAR + bankCard.substr(bankCard.length - 4)
    }
    return bankCard
}

/**
 * 邮箱脱敏
 * @param email email
 * @return 脱敏后的email
 */
export function desensitizeEmail(email) {
    let emailArray = []
    if (!_.isEmpty(email)) {
        if (email.indexOf("@") > 0) {
            emailArray = email.split("@")
            let prefix = emailArray && emailArray[0]
            if (prefix.length > 5) {
                prefix = prefix.substr(0, 2) + STRING_STAR + prefix.substr(prefix.length - 2)
            } else if (prefix.length > 2) {
                prefix = prefix.substr(0, 1) + STRING_STAR + prefix.substr(prefix.length - 1)
            }
            emailArray[0] = prefix
        }
    }
    return emailArray[0] !== undefined ? emailArray[0] + '@' + emailArray[1] : ''
}

/**
 * 敏感信息脱敏脱敏
 * @param str 银敏感信息
 * @return 脱敏后的敏感信息
 */
export function desensitizeCommon(str) {
    if (!_.isEmpty(str) && str.length >= 5) {
        str = str.substr(0, 2) + STRING_STAR + str.substr(str.length - 2)
    }
    return str
}